package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.Notice;
import org.springblade.modules.app.service.INoticeService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 工单信息表 控制器
 *
 * @author BladeX
 * @since 2020-10-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/notice")
@Api(value = "工单信息表", tags = "工单信息表接口")
public class NoticeController extends BladeController {

    private final INoticeService noticeService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    public R<Notice> detail(Notice notice) {
        Notice detail = noticeService.getOne(Condition.getQueryWrapper(notice));
        return R.data(detail);
    }

    /**
     * 分页 工单信息表
     */
    @GetMapping("/list")
    public R<IPage<Notice>> list(Notice notice, Query query) {
        IPage<Notice> pages = noticeService.page(Condition.getPage(query), Condition.getQueryWrapper(notice));
        return R.data(pages);
    }

    /**
     * 自定义分页 工单信息表
     */
    @GetMapping("/page")
    public R<IPage<Notice>> page(Page page, Notice notice) {
        BladeUser bladeUser = AuthUtil.getUser();
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.and(wrapper -> wrapper.eq(Notice::getUserid, bladeUser.getUserId()));
        queryWrapper.orderByDesc(Notice::getCtime);
        IPage<Notice> pages = noticeService.page(page, queryWrapper);
        return R.data(pages);
    }


    /**
     * 新增 工单信息表
     */
    @PostMapping("/save")
    public R save(@Valid @RequestBody Notice notice) {
        return R.status(noticeService.save(notice));
    }

    /**
     * 修改 工单信息表
     */
    @PostMapping("/update")
    public R update(@Valid @RequestBody Notice notice) {
        return R.status(noticeService.updateById(notice));
    }

    /**
     * 新增或修改 工单信息表
     */
    @PostMapping("/submit")
    public R submit(@Valid @RequestBody Notice notice) {
        return R.status(noticeService.saveOrUpdate(notice));
    }


    /**
     * 删除 工单信息表
     */
    @PostMapping("/remove")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(noticeService.removeByIds(Func.toLongList(ids)));
    }


}
